<template>
    <div class="in-wrap">
        <!-- 公共头引入 -->
        <header id="header">
            <section class="container">
                <h1 id="logo">
                    <a href="/" title="小麦穗">
                        <img src="~/assets/img/logo/3.png" width="100%" alt="小麦穗" />
                    </a>
                </h1>
                <div class="h-r-nsl">
                    <ul class="nav">
                        <router-link to="/" tag="li" active-class="current" exact>
                            <a>首页</a>
                        </router-link>
                        <router-link to="/course" tag="li" active-class="current">
                            <a>课程</a>
                        </router-link>
                        <router-link to="/teacher" tag="li" active-class="current">
                            <a>名师</a>
                        </router-link>
                        <router-link to="/classroom" tag="li" active-class="current">
                            <a>报班</a>
                        </router-link>
                        <router-link to="/order" tag="li" active-class="current">
                            <a>订单</a>
                        </router-link>
                        <router-link to="/selfCourse" tag="li" active-class="current">
                            <a>我的</a>
                        </router-link>
                    </ul>
                    <!-- / nav -->
                    <!-- / nav -->
                    <ul class="h-r-login">
                        <li v-if="!loginInfo.id" id="no-login">
                            <a href="/login" title="登录">
                                <em class="icon18 login-icon">&nbsp;</em>
                                <span class="vam ml5">登录</span>
                            </a>
                            |
                            <a href="/register" title="注册">
                                <span class="vam ml5">注册</span>
                            </a>
                        </li>
                        <li v-if="loginInfo.id" id="is-login-one" class="mr10">
                            <a
                                id="headerMsgCountId"
                                href="#"
                                title="消息"
                                @click.prevent="drawer=true"
                            >
                                <el-badge
                                    :value="$store.state.newsNums"
                                    :max="99"
                                    :is-dot="!$store.state.newsNums"
                                >
                                    <em class="icon18 news-icon">&nbsp;</em>
                                </el-badge>
                            </a>
                            <q class="red-point" style="display: none">&nbsp;</q>
                        </li>
                        <li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
                            <a href="/ucenter" title>
                                <img
                                    :src="loginInfo.avatar"
                                    width="30"
                                    height="30"
                                    class="vam picImg"
                                    alt
                                />
                                <el-tooltip
                                    class="item"
                                    effect="light"
                                    :content="loginInfo.nickname"
                                    placement="top-start"
                                >
                                    <span id="userName" class="vam disIb">{{ loginInfo.nickname }}</span>
                                </el-tooltip>
                            </a>
                            <a
                                href="javascript:void(0);"
                                title="退出"
                                @click="logout()"
                                class="ml5"
                            >退出</a>
                        </li>
                        <!-- /未登录显示第1 li；登录后显示第2，3 li -->
                    </ul>
                    <aside class="h-r-search">
                        <label class="h-r-s-box">
                            <input
                                type="text"
                                placeholder="输入你想学的课程"
                                v-model="searchName"
                                @keyup.enter="searchCourse()"
                            />
                            <button class="s-btn" @click="searchCourse()">
                                <em class="icon18">&nbsp;</em>
                            </button>
                        </label>
                    </aside>
                </div>
                <aside class="mw-nav-btn">
                    <div class="mw-nav-icon"></div>
                </aside>
                <div class="clear"></div>
            </section>

            <!-- Form -->
            <el-dialog title="绑定手机号" :visible.sync="dialogFormVisible" width="30%">
                <el-form :model="loginInfo" ref="bingMobileForm" status-icon :rules="rules">
                    <el-form-item
                        label="手机号"
                        label-width="120px"
                        prop="mobile"
                        :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]"
                    >
                        <el-input style="width:70%" v-model="loginInfo.mobile"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="pass" label-width="120px">
                        <el-input style="width:70%" type="password" v-model="loginInfo.password"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass" label-width="120px">
                        <el-input style="width:70%" type="password" v-model="loginInfo.checkPass"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="bindingMobile()">确 定</el-button>
                </div>
            </el-dialog>
        </header>
        <!-- /公共头引入 -->

        <!--*******************推送抽屉**********************-->
        <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false" size="25%">
            <push-schedule></push-schedule>
        </el-drawer>
        <nuxt />

        <!-- 公共底引入 -->
        <footer id="footer">
            <section class="container">
                <div class>
                    <h4 class="hLh30">
                        <span class="fsize18 f-fM c-999">友情链接</span>
                    </h4>
                    <ul class="of flink-list">
                        <li>
                            <a
                                href="https://blog.csdn.net/weixin_52130455?type=blog"
                                title="小麦穗"
                                target="_blank"
                            >小麦穗</a>
                        </li>
                    </ul>
                    <div class="clear"></div>
                </div>
                <div class="b-foot">
                    <section class="fl col-7">
                        <section class="mr20">
                            <section class="b-f-link">
                                <a
                                    href="https://blog.csdn.net/weixin_52130455?type=blog"
                                    title="关于我们"
                                    target="_blank"
                                >关于我们</a>|
                                <a
                                    href="https://blog.csdn.net/weixin_52130455?type=blog"
                                    title="联系我们"
                                    target="_blank"
                                >联系我们</a>|
                                <a
                                    href="https://blog.csdn.net/weixin_52130455?type=blog"
                                    title="帮助中心"
                                    target="_blank"
                                >帮助中心</a>|
                                <a
                                    href="https://blog.csdn.net/weixin_52130455?type=blog"
                                    title="资源下载"
                                    target="_blank"
                                >资源下载</a>|
                                <span>服务热线：19895985605</span>
                                <span>Email：1822172305@qq.com</span>
                            </section>
                            <section class="b-f-link mt10">
                                <span>如果你努力去发现美好，美好会发现你;如果你努力去尊重他人，你也会获得别人尊重;如果你努力去帮助他人，你也会得到他人的帮助。.</span>
                            </section>
                        </section>
                    </section>
                    <aside class="fl col-3 tac mt15">
                        <section class="gf-tx">
                            <span>
                                <img src="~/assets/img/wx-icon.png" alt />
                            </span>
                        </section>
                        <section class="gf-tx">
                            <span>
                                <img src="~/assets/img/wb-icon.png" alt />
                            </span>
                        </section>
                    </aside>
                    <div class="clear"></div>
                </div>
            </section>
        </footer>
        <!-- /公共底引入 -->
    </div>
</template>
<script>
import "~/assets/css/reset.css";
import "~/assets/css/theme.css";
import "~/assets/css/global.css";
import "~/assets/css/global2.css";
import "~/assets/css/web.css";
import "~/assets/css/base.css";
import "~/assets/css/activity_tab.css";
import "~/assets/css/bottom_rec.css";
import "~/assets/css/nice_select.css";
import "~/assets/css/order.css";
import "~/assets/css/swiper-3.3.1.min.css";
import "~/assets/css/pages-weixinpay.css";
import cookie from "js-cookie";
import defaultAvatar from "~/assets/img/avatar-boy.gif";
import { updateUser, getLoginInfo, logout } from "@/api/ucenter/user";
import PushSchedule from "@/components/PushSchedule";
export default {
    components: { PushSchedule },
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.loginInfo.checkPass !== "") {
                    this.$refs.bingMobileForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.loginInfo.password) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            drawer: false,

            searchName: "",
            token: "",
            loginInfo: {
                id: "",
                age: "",
                avatar: "",
                mobile: "",
                nickname: "",
                password: "",
                checkPass: "",
                sex: "",
            },
            rules: {
                pass: [{ validator: validatePass, trigger: "blur" }],
                checkPass: [{ validator: validatePass2, trigger: "blur" }],
            },
            dialogFormVisible: false,
        };
    },
    methods: {
        searchCourse() {
            this.$store.commit("setSearch", this.searchName);
            this.$router.push({
                path: "/course/",
            });
        },
        showInfo() {
            //debugger
            var jsonStr = cookie.get("edu_user");
            //alert(jsonStr)
            if (jsonStr) {
                this.loginInfo = JSON.parse(jsonStr);
                if (!this.loginInfo.avatar) {
                    this.loginInfo.avatar = defaultAvatar;
                }
            }
        },
        bindingMobile() {
            this.$refs.bingMobileForm.validate((valid) => {
                if (valid) {
                    updateUser(this.loginInfo).then((response) => {
                        cookie.set("edu_user", this.loginInfo, {
                            domain: "localhost",
                        });
                        this.dialogFormVisible = false;
                        this.$message({
                            message: "绑定成功",
                            type: "success",
                        });
                    });
                } else {
                    return false;
                }
            });
        },
        checkPhone(rule, value, callback) {
            //debugger
            if (!/^1[3456789]\d{9}$/.test(value)) {
                return callback(new Error("手机号码格式不正确"));
            }
            return callback();
        },
        logout() {
            logout();
            //debugger
            cookie.set("edu_user", "", { domain: "localhost" });
            cookie.set("edu_token", "", { domain: "localhost" });
            //跳转页面
            window.location.href = "/";
        },
        wxLogin() {
            cookie.set("edu_token", this.token, { domain: "localhost" });
            getLoginInfo().then((response) => {
                this.loginInfo = response.data.user;
                //将用户信息记录cookie
                cookie.set("edu_user", this.loginInfo, {
                    domain: "localhost",
                });
                if (!this.loginInfo.mobile) {
                    this.dialogFormVisible = true;
                }
                this.showInfo();
            });
        },
    },
    created() {},
    mounted() {
        //获取路径中的token
        let token = this.$route.query.token;
        if (token) {
            this.token = token;
            this.wxLogin();
        } else {
            this.showInfo();
        }
    },
    beforeDestroy() {},
};
</script>
<style scoped>
</style>